package org.syncsoft.overalt.communication.impl;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.syncsoft.overalt.common.api.Configuration;
import org.syncsoft.overalt.common.api.Parameter;
import org.syncsoft.overalt.communication.exception.ConnectionException;
import org.syncsoft.overalt.communication.exception.LoginException;

/**
 * 
 * @author tptfc
 *
 */
public class ClosedConnectionStatus extends AbstractConnectionStatus {
	private String login;
	private String password;
	private String urlConnection;
	
	public ClosedConnectionStatus(DefaultHttpClient httpClient) {
		super(httpClient);
	}
	
	public ClosedConnectionStatus(String login, String password, String urlConnection, DefaultHttpClient httpClient) {
		super(httpClient);
		setLogin(login);
		setPassword(password);
		setUrlConnection(urlConnection);
	}

	/*
	 * (non-Javadoc)
	 * @see org.syncsoft.overalt.communication.api.IConnectionStatus#open()
	 */
	@Override
	public synchronized void open() throws ConnectionException {
		
		try {
			HttpResponse response = getHttpClient().execute(getLoginHttpPost());
			response.getEntity().consumeContent();
			
			if (!isConnected()) {
				throw new LoginException();
			}
		} catch (ClientProtocolException e) {
			throw new ConnectionException(e);
		} catch (IOException e) {
			throw new ConnectionException(e);
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.syncsoft.overalt.communication.api.IConnectionStatus#close()
	 */
	@Deprecated
	@Override
	public synchronized void close() {}

	/*
	 * (non-Javadoc)
	 * @see org.syncsoft.overalt.communication.api.IConnectionStatus#get(java.lang.String, java.util.Map)
	 */
	@Override
	public HttpResponse get(String uri, List<Parameter> parameters) throws ConnectionException {
		throw new ConnectionException();
	}

	/*
	 * (non-Javadoc)
	 * @see org.syncsoft.overalt.communication.api.IConnectionStatus#post(java.lang.String, java.util.Map)
	 */
	@Override
	public HttpResponse post(String uri, List<Parameter> parameters) throws ConnectionException {
		throw new ConnectionException();
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.syncsoft.overalt.communication.api.IConnectionStatus#postMultipart(java.lang.String, java.util.Map, java.lang.String, java.io.File)
	 */
	@Override
	public HttpResponse postMultipart(String uri, List<Parameter> parameters, String fileEntityName, File file) throws ConnectionException {
		throw new ConnectionException();
	}
	
	/**
	 * @precondition (login && password && urlConnection) != null
	 * 
	 * @return the HttpPost with login action data
	 * @throws ConnectionException 
	 */
	protected HttpPost getLoginHttpPost() throws ConnectionException {
		HttpPost httpPost = new HttpPost(getUrlConnection());
		
		try {
			List<Parameter> parameters = new ArrayList<Parameter>();
			parameters.add(new Parameter(Configuration.getLoginQueryParameterName(), getLogin()));
			parameters.add(new Parameter(Configuration.getPasswordQueryParameterName(), getPassword()));
			
			httpPost.setEntity(buildPostRequestParameters(parameters));
		} catch (UnsupportedEncodingException e) {
			throw new ConnectionException(e);
		}
		
		return httpPost;
	}

	protected String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	protected String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	protected String getUrlConnection() {
		return urlConnection;
	}

	public void setUrlConnection(String urlConnection) {
		this.urlConnection = urlConnection;
	}
}
